package wifiunion.com.notice.utils.glideutils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

import wifiunion.com.notice.R;

public class NewGlideCircleHasBackgroudTransform extends BitmapTransformation {

    /**
     * 0.null
     * 1.blue
     * 2.red
     * 3.yellow
     * 4.trans
     */
    private int mBackgroudColor;

    public NewGlideCircleHasBackgroudTransform(Context context) {
        super(context);
    }

    private int mType;

    private Context mContext;

    private int mSize;

    public NewGlideCircleHasBackgroudTransform(Context context, int type, int size) {
        super(context);
        mType = type;
        mContext = context;
        mSize = size;
        switch (type) {
            case 0:
                mBackgroudColor = context.getResources().getColor(R.color.start_circle_blue);
                break;
            case 1:
                mBackgroudColor = context.getResources().getColor(R.color.start_circle_blue);
                break;
            case 2:
                mBackgroudColor = context.getResources().getColor(R.color.start_circle_red);
                break;
            case 3:
                mBackgroudColor = context.getResources().getColor(R.color.start_circle_yellow);
                break;
            case 4:
                mBackgroudColor = context.getResources().getColor(R.color.start_circle_blue);
                break;
            default:
                mBackgroudColor = context.getResources().getColor(R.color.start_circle_blue);
                break;
        }
    }

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        return circleCrop(pool, toTransform);
    }

    private Bitmap circleCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;
//        Log.e(Constants.TAG,"source ==null  "+(source==null ) + source.getWidth()+"   "+source.getHeight());

//        if (source.getHeight() < mSize && source.getWidth() < mSize) {
//
//        }

        int size = Math.min(source.getWidth(), source.getHeight());


        int x = (source.getWidth() - size) / 2;
        int y = (source.getHeight() - size) / 2;

        // TODO this could be acquired from the pool too
//        Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
        Bitmap newsquared = null;
        Bitmap result = null;
        float r = 0.0f;
//        if (size > mSize) {
//            newsquared = Bitmap.createBitmap(source, x, y, size, size);
//
//            result = pool.get(size, size, Bitmap.Config.ARGB_8888);
//            if (result == null) {
//                result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
//            }
//        } else {
        newsquared = Bitmap.createScaledBitmap(source, mSize, mSize, true);
        result = pool.get(mSize, mSize, Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(mSize, mSize, Bitmap.Config.ARGB_8888);
        }
//        }
        r = mSize / 2f;
//        Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
//        if (result == null) {
//            result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
//        }
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
//        float r = size / 2f;
        paint.setColor(mBackgroudColor);
        LinearGradient gradient;
        switch (mType) {
            case 0:
            case 1:
                gradient = new LinearGradient(0, 0, r, r, mContext.getResources().getColor(R.color.start_circle_blue), mContext.getResources().getColor(R.color.end_circle_blue), Shader.TileMode.MIRROR);
                break;
            case 2:
                gradient = new LinearGradient(0, 0, r, r, mContext.getResources().getColor(R.color.start_circle_red), mContext.getResources().getColor(R.color.end_circle_red), Shader.TileMode.MIRROR);
                break;
            case 3:
                gradient = new LinearGradient(0, 0, r, r, mContext.getResources().getColor(R.color.start_circle_yellow), mContext.getResources().getColor(R.color.end_circle_yellow), Shader.TileMode.MIRROR);
                break;
            case 4:
                gradient = new LinearGradient(0, 0, r, r, mContext.getResources().getColor(R.color.start_circle_blue), mContext.getResources().getColor(R.color.end_circle_blue), Shader.TileMode.MIRROR);
                break;
            default:
                gradient = new LinearGradient(0, 0, r, r, mContext.getResources().getColor(R.color.start_circle_blue), mContext.getResources().getColor(R.color.end_circle_blue), Shader.TileMode.MIRROR);
                break;
        }
//        LinearGradient gradient = new LinearGradient(0, 0, (r-10)*2, (r-10)*2, 0xffff2959, 0xffff0c45, Shader.TileMode.MIRROR);

        paint.setShader(gradient);
        canvas.drawCircle(r, r, (r), paint);

        paint.reset();

        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setShader(new BitmapShader(newsquared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawCircle(r, r, (r - mContext.getResources().getDimension(R.dimen.dp_15)), paint);


        return result;
    }


    @Override
    public String getId() {
//        Random rd = new Random();
//        ;
//        return String.valueOf(rd.nextInt());
        return getClass().getName() + " " + mType;
    }

}